/**
 * (C)2013 Exlu Corporation. All rights reserved.
 * 注意事項   :
 */
package cn.exlu.wea.io.directory.file;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

/**
 * Properties文件读取器
 * @author Mark
 * @version 1.0.1.1
 * @CreateTime 2013-6-10
 */
public class FilePropertiesReader {
    private Properties properties;

    /**
     * 读取指定文件的构造函数
     * @param file 指定文件
     * @throws IOException 读取文件异常
     */
    public FilePropertiesReader(File file) throws IOException{
        BufferedInputStream buffInputStream=new BufferedInputStream(new FileInputStream(file));
        
        properties=new Properties();
        properties.load(buffInputStream);
        buffInputStream.close();
    }
    
    /**
     * 读取指定文件的构造函数
     * @param file 指定文件
     * @param encoding 编码
     * @throws IOException 读取文件异常
     */
    public FilePropertiesReader(File file,String encoding) throws IOException{
        BufferedReader buffReader=new BufferedReader(new InputStreamReader(new FileInputStream(file),encoding));
        
        properties=new Properties();
        properties.load(buffReader);
    }
    
    /**
     * 读取指定文件的构造函数
     * @param inputStream 输入流
     * @throws IOException 读取文件异常
     */
    public FilePropertiesReader(InputStream inputStream) throws IOException{
        BufferedInputStream buffInputStream=new BufferedInputStream(inputStream);
        properties=new Properties();
        properties.load(buffInputStream);
        buffInputStream.close();
    }
    
    /**
     * 获取对应的属性值
     * @param propName 属性名
     * @return 属性值
     */
    public String getPropValue(String propName){
        return properties.getProperty(propName);
    }
 
    /**
     * 将Properties文件值放入Map中
     * @param map 要放入的Map
     * @return 赋值后的Map
     */
    public Map<String,String> setPropToMap(Map<String,String> map){
        Set<Entry<Object, Object>> set = properties.entrySet();
        Iterator<Entry<Object, Object>> iterator = set.iterator();
        while (iterator.hasNext()) {
            Entry<Object,Object> entry = iterator.next();
            map.put((String)entry.getKey(), (String)entry.getValue());
        }
        return map;
    }
}
